﻿using System;
using RaytracerLib.MathLib;
using YAXLib;

namespace RaytracerLib.Layers
{
    public abstract class SolidLayer : Layer
    {
        protected double m_level_value;
        protected ColorFloat m_level_color;

        public override ColorFloat GetColor(Vector2 a_uv, Intersection a_intersection)
        {
            return m_level_color;
        }

        public override Tangents GetTangents(Vector2 a_uv, Vector2 a_tx, Vector2 a_ty,
            Intersection a_intersection)
        {
            throw new NotImplementedException();
        }

        public override double GetValue(Vector2 a_uv, Intersection a_intersection)
        {
            return m_level_value;
        }
    }

    public class SolidColorLayer : SolidLayer
    {
        public SolidColorLayer(ColorFloat a_color)
        {
            Color = a_color;
        }

        [YAXNode]
        public ColorFloat Color
        {
            get
            {
                return m_level_color;
            }
            set
            {
                m_level_color = value;
                m_level_value = m_level_color.Avg;
            }
        }

        public override void RenderStart(RenderStartPhase a_phase)
        {
            if (a_phase == RenderStartPhase.SRGBToLinear)
                Color = Gamma.SRGBToLinear(Color);

            base.RenderStart(a_phase);
        }
    }

    public class SolidValueLayer : SolidLayer
    {
        public SolidValueLayer(double a_value)
        {
            Value = a_value;
        }

        [YAXNode]
        public double Value
        {
            get
            {
                return m_level_value;
            }
            set
            {
                m_level_value = value;
                m_level_color = new ColorFloat(value);
            }
        }
    }
}